package com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiPensionAdapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.MonthlyComboItemLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.httpbean.pension.MonthlyComboBean;

import java.util.ArrayList;
import java.util.List;


/**
 * author: Administrator
 * date: 2017/5/13.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiPensionAdapter
 */

public class MonthlyComboListAdapter extends RecyclerView.Adapter<MonthlyComboListAdapter.BindingHolder> {
    Context context;
    List<MonthlyComboBean.ShoplistBean> MonthlyComboList=new ArrayList<>();
    LayoutInflater inflater;
    MonthlyComboItemLayoutBinding binding;
    private OnItemClickListener mOnItemClickListener;


    public MonthlyComboListAdapter(Context context, List<MonthlyComboBean.ShoplistBean> MonthlyComboList) {
        this.context=context;
        this.MonthlyComboList=MonthlyComboList;
        inflater=LayoutInflater.from(context);
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        binding= DataBindingUtil.inflate(inflater,R.layout.monthly_combo_item_layout,parent,false);
        BindingHolder holder=new BindingHolder(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(final BindingHolder holder, int position) {
        MonthlyComboBean.ShoplistBean bean=MonthlyComboList.get(position);
        Glide.with(context).load(bean.getImagePath()).into(binding.imgMonthlyCombo);
        binding.monthlyComboItemContent.setText(bean.getProductName());
        binding.monthlyComboPrice.setText("￥"+bean.getPrice());

        if (mOnItemClickListener != null) {
            /**
             * 这里加了判断，itemViewHolder.itemView.hasOnClickListeners()
             * 目的是减少对象的创建，如果已经为view设置了click监听事件,就不用重复设置了
             * 不然每次调用onBindViewHolder方法，都会创建一个监听事件对象，增加了内存的开销
             */
            if (!holder.itemView.hasOnClickListeners()) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = holder.getPosition();
                        mOnItemClickListener.onItemClick(v, position);
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return MonthlyComboList.size();
    }

    public class BindingHolder extends RecyclerView.ViewHolder {
        MonthlyComboItemLayoutBinding binding;

        public MonthlyComboItemLayoutBinding getBinding() {
            return binding;
        }

        public void setBinding(MonthlyComboItemLayoutBinding binding) {
            this.binding = binding;
        }

        public BindingHolder(View itemView) {
            super(itemView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
